/*
 * 文件名：dataDictionaryControl.java	 
 * 时     间：下午1:35:28
 * 作     者：tianJun       
 * 版     权： 2012-2022 湖南智软网络科技, 公司保留所有权利.
 * 联     系：http://www.073586.com/
 */
package com.local.controller.index;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.cz073586.tools.util.Pinyin;
import com.local.manager.ConfigManager;
import com.local.model.DataDictionary;
import com.local.service.face.DataDictionaryService;
import com.qianhai.common.constants.CommonContans;
import com.qianhai.common.controller.BaseController;

/**
 * 类描述:这里是类描述
 * 
 * @ClassName: dataDictionaryControl
 * @author tianJun
 * @date 2016年8月18日 下午1:35:28
 */
@RestController
@RequestMapping(value = "/com/data", method = RequestMethod.POST)
public class DataDictionaryController extends BaseController
{
	@Autowired
	private DataDictionaryService dataDictionaryService;
	List<Object> dataTree = new ArrayList<Object>();
	
	/**
	 * 
	 * 方法描述：获取字典的上级目录的所有树形结构
	 * 
	 * @Title: getAllTree
	 * @param @return
	 * @return ModelMap
	 * @user tianJun 2016年8月29日
	 * @throws
	 */
	@RequestMapping(value = "/getAllTree.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap getAllTree()
	{
		ModelMap resultMap = new ModelMap();
		try
		{
			List<Map<Object, Object>> returnTree = dataDictionaryService
					.getAllTree();
			resultMap.put(CommonContans.STRUTS_DATA, returnTree);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// TODO Auto-generated catch block
			this.printExction(resultMap, e);
		}
		return resultMap;
		
	}
	
	/**
	 * 
	 * 方法描述：获取字典的上级目录的树形结构
	 * 
	 * @Title: getTree
	 * @param @return
	 * @return ModelMap
	 * @user tianJun 2016年8月29日
	 * @throws
	 */
	@RequestMapping(value = "/getTree.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap getTree(@RequestParam String attr1)
	{
		ModelMap resultMap = new ModelMap();
		try
		{
			List<String> dataList = dataDictionaryService.getTreeRoot(attr1);
			dataTree.add(dataList);
			List<Object> returnTree = getAllTree(dataList);
			resultMap.put(CommonContans.STRUTS_DATA, returnTree);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	};
	
	/**
	 * 方法描述：查询所有的树
	 * 
	 * @Title: getAllTree
	 * @param @param list
	 * @param @return
	 * @return List<Object>
	 * @user tianJun 2016年8月30日
	 * @throws
	 */
	public List<Object> getAllTree(List<String> list)
	{
		try
		{
			if (list.isEmpty())
			{
				return dataTree;
			} else
			{
				List<String> list2 = dataDictionaryService.getTree(list);
				dataTree.add(list2);
				list = list2;
				
				getAllTree(list);
			}
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			e.printStackTrace();
		}
		return dataTree;
	}
	
	/**
	 * 
	 * 方法描述：获取配置文件中的输入框类型，以json格式输出
	 * 
	 * @Title: getJsonString
	 * @param @return
	 * @return ModelMap
	 * @user tianjun 2016年8月29日
	 * @throws
	 */
	
	@RequestMapping(value = "/getJsonString.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap getJsonString()
	{
		ModelMap resultMap = new ModelMap();
		try
		{
			Map jsonString = ConfigManager.getInstance().dicMap;
			resultMap.put(CommonContans.STRUTS_DATA, jsonString);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	};
	
	/**
	 * 
	 * 方法描述：获取全部字典信息
	 * 
	 * @Title: selectAll
	 * @param @param dataDic_page
	 * @param @return
	 * @return ModelMap
	 * @user tianjun 2016年8月29日
	 * @throws
	 */
	@RequestMapping(value = "/selectAll.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap selectAll(@RequestBody DataDictionary dataDic_page)
	{
		ModelMap resultMap = new ModelMap();
		try
		{
			if (dataDic_page.getCurrentPage() != 1)
			{
				dataDic_page
						.setCurrentPage((dataDic_page.getCurrentPage() - 1) * 10);
			} else
			{
				dataDic_page
						.setCurrentPage((dataDic_page.getCurrentPage() - 1));
			}
			List<DataDictionary> dataDictionary = dataDictionaryService
					.selectAll(dataDic_page);
			Integer totalRecords = dataDictionaryService.getCount();
			Integer totalPages;
			if (totalRecords / 10 == 0)
			{
				totalPages = totalRecords / 10;
			} else
			{
				totalPages = totalRecords / 10 + 1;
			}
			resultMap.put("totalRecords", totalRecords);
			resultMap.put("totalPages", totalPages);
			resultMap.put(CommonContans.STRUTS_DATA, dataDictionary);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	};
	
	/**
	 * 
	 * 方法描述：根据uuid选择
	 * 
	 * @Title: selectByUuid
	 * @param @param uuid
	 * @param @return
	 * @return ModelMap
	 * @user tianJun 2016年8月29日
	 * @throws
	 */
	@RequestMapping(value = "/selectByUuid.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap selectByUuid(@RequestParam String uuid)
	{
		ModelMap resultMap = new ModelMap();
		try
		{
			DataDictionary dataDictionary = dataDictionaryService
					.selectByUuid(uuid);
			resultMap.put(CommonContans.STRUTS_DATA, dataDictionary);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	};
	
	/**
	 * 
	 * 方法描述：根据code选择
	 * 
	 * @Title: getValueByKey
	 * @param @param code
	 * @param @return
	 * @return ModelMap
	 * @user tianJun 2016年8月29日
	 * @throws
	 */
	@RequestMapping(value = "/getValueByKey.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap getValueByKey(String code)
	{
		ModelMap resultMap = new ModelMap();
		try
		{
			List<DataDictionary> dataDictionary = dataDictionaryService
					.getValueByKey(code);
			resultMap.put(CommonContans.STRUTS_DATA, dataDictionary);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	};
	
	/**
	 * 
	 * 方法描述：更新
	 * 
	 * @Title: updateByUuid
	 * @param @param dataDictionary
	 * @param @return
	 * @return ModelMap
	 * @user tianJun 2016年8月29日
	 * @throws
	 */
	@RequestMapping(value = "/updateByUuid.action", consumes = "application/json", produces = "application/json")
	public ModelMap updateByUuid(@RequestBody DataDictionary dataDictionary)
	{
		ModelMap resultMap = new ModelMap();
		try
		{
			String attr2 = dataDictionary.getAttr2();
			String code = attr2 + "_"
					+ Pinyin.getPinYin(dataDictionary.getFieldName());
			dataDictionary.setCode(code);
			dataDictionary.setAttr1(code);
			dataDictionaryService.updateByUuid(dataDictionary);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	};
	
	/**
	 * 
	 * 方法描述：删除
	 * 
	 * @Title: deleteByUuid
	 * @param @param uuid
	 * @param @return
	 * @return ModelMap
	 * @user tianJun 2016年8月29日
	 * @throws
	 */
	@RequestMapping(value = "/deleteById.action", consumes = "application/json", produces = "application/json")
	public ModelMap deleteByUuid(@RequestParam String attr1)
	{
		ModelMap resultMap = new ModelMap();
		try
		{
			dataDictionaryService.getPid(attr1);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	};
	
	/**
	 * 
	 * 方法描述：添加
	 * 
	 * @Title: insert
	 * @param @param dataDictionary
	 * @param @return
	 * @return ModelMap
	 * @user tianJun 2016年8月29日
	 * @throws
	 */
	@RequestMapping(value = "/insert.action", consumes = "application/json", produces = "application/json")
	public ModelMap insert(@RequestBody DataDictionary dataDictionary)
	{   	
		ModelMap resultMap = new ModelMap();
		try
		{
			String uuid = UUID.randomUUID().toString();
			dataDictionary.setUuid(uuid);
			String code = dataDictionary.getCode();
			String filedName = Pinyin.getPinYin(dataDictionary.getFieldName());
			if (code == null || code == "")
			{
				dataDictionary.setCode(filedName);
			} else
			{
				dataDictionary.setCode(code + "_" + filedName);
			}			
			dataDictionary.setAttr1(code + "_" + filedName);
			//生成唯一的树节点ID
			//dataDictionary.setAttr1(Long.toString(System.currentTimeMillis()));
			
			dataDictionaryService.insert(dataDictionary);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	};
}
